package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.utils.UserContext;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.query.TagQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITagService;
import cn.wolfcode.trip.base.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import java.util.List;

@Controller
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private ITagService tagService;
    @Autowired
    private ServletContext ctx;


    @GetMapping
    @ResponseBody
    public Object getCommendStrategy(@ModelAttribute("qo") StrategyQueryObject qo){
        qo.setPageSize(0);
        return strategyService.query(qo);
    }



  /*  @GetMapping("/{regionId}")
    @ResponseBody
    public void get(@ModelAttribute("qo") StrategyQueryObject qo, HttpServletResponse resp) throws Exception {

        //查询出当前地区所有的攻略
        PageInfo alls = strategyService.query(qo);

        //查询出当前地区的攻略状态为推荐的攻略(不需要分页)
        qo.setPageSize(0);
        qo.setState(2L);
        PageInfo commends = strategyService.query(qo);

        //利用freemarker:数据+模板--->输出

        //创建配置对象
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        //获取模板的文件位置
        cfg.setDirectoryForTemplateLoading(new File(ctx.getRealPath("templates")));
        //设置文件模板
        Template temp = cfg.getTemplate("strategy.ftl");
        //合并模板和数据模型
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("allStrategies",alls);
        map.put("commendStrategies",commends);
        temp.process(map, resp.getWriter());

    }*/

    //查询所有攻略的标题---------------------------------------
    @GetMapping("/search")
    @ResponseBody
    public Object getStrategies(){
        return strategyService.searchTitle();
    }

    //根据标题搜索攻略跳转--------------------------------------
    @GetMapping("/titles")
    @ResponseBody
    public Object getStrategies(String title){
        return strategyService.selectStrategyByTitle(title);
    }


    //根据id查询攻略
    @GetMapping("/{id}")
    @ResponseBody
    public Object getStrategyById(@PathVariable Long id){
        return strategyService.selectByStrategyId(id);
    }

    //根据id查询大攻略下的所有目录分类
    @GetMapping("/{id}/catalogs")
    @ResponseBody
    public Object getStrategyCatalogByStrategyId(@PathVariable Long id){
        return strategyCatalogService.getStrategyCatalogByStrategyId(id);
    }

    //根据id查询大攻略下的所有目录分类
    @GetMapping("/{id}/comments")
    @ResponseBody
    public Object getStrategyCommentsByStrategyId(@PathVariable Long id){
        return strategyCommentService.selectCommentsByStrategyId(id);
    }

    //根据id查询大攻略下的所有点评的标签
    @GetMapping("/{id}/tags")
    @ResponseBody
    public Object getTagsByStrategyId(@ModelAttribute("qo")TagQueryObject qo){
        return tagService.query(qo);
    }

    //保存某大攻略下骡友点评
    @PostMapping("/comments")
    @ResponseBody
    public Object saveComment(StrategyComment comment){
        comment.setUser(UserContext.getCurrentUser());
         strategyCommentService.saveComment(comment);
         return new JSONResult();
    }

    @GetMapping("/{strategyId}/likeCount")
    @ResponseBody
    public Object getLikeCount(@PathVariable Long strategyId,Long userId){
        int count = strategyService.getLikeCount(strategyId);
        JSONResult result = new JSONResult();
        result.setObj(count);
        if (userId!=null){

            if (strategyService.queryIfexist(strategyId,userId)==1){
                return result;
            }else {
                result.setSuccess(false);
                return result;
            }
        }else {
            result.setSuccess(false);
            return result;
        }
    }
    @PostMapping("/like/saveLike")
    @ResponseBody
    public Object saveLike(Long strategyId,Long userId){
        JSONResult result = new JSONResult();
        try {
            strategyService.saveLike(userId,strategyId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @DeleteMapping("/like/deleteLike")
    @ResponseBody
    public Object delete( Long strategyId,Long userId){
        JSONResult result = new JSONResult();
        try {
            strategyService.deleteLike(userId,strategyId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }




}
